#include "NeoSlider.h"
#include "private/NeoSliderPrivate.h"
#include <QMouseEvent>

NEO_PUBLIC_PROPERTY_CREATE_CPP(NeoSlider, QColor, IndicatorColor)
NEO_PUBLIC_PROPERTY_CREATE_CPP(NeoSlider, QColor, TrackColor)
NEO_PUBLIC_PROPERTY_CREATE_CPP(NeoSlider, QColor, BackgroundColor)

NeoSlider::NeoSlider(QWidget *parent)
    : QSlider(parent), d_ptr(new NeoSliderPrivate())
{
    NEO_PUBLIC_POINT(NeoSlider)
    d->q_ptr = this;
    this->setOrientation(Qt::Horizontal);
}

NeoSlider::NeoSlider(Qt::Orientation orientation, QWidget *parent)
    : QSlider(orientation, parent), d_ptr(new NeoSliderPrivate())
{
    NEO_PUBLIC_POINT(NeoSlider)
    d->q_ptr = this;
}

NeoSlider::~NeoSlider() = default;

void NeoSlider::mousePressEvent(QMouseEvent *event)
{
    NEO_PUBLIC_POINT(NeoSlider)
    if (event->button() == Qt::LeftButton)
    {
        this->setSliderPosition(d->mousePositionForValue(event->localPos()));
        this->update();
        event->accept();
        return;
    }
    return QSlider::mousePressEvent(event);
}

void NeoSlider::mouseMoveEvent(QMouseEvent *event)
{
    NEO_PUBLIC_POINT(NeoSlider)
    if (event->buttons() == Qt::LeftButton)
    {
        this->setSliderPosition(d->mousePositionForValue(event->localPos()));
        this->update();
        event->accept();
        return;
    }
    return QSlider::mouseMoveEvent(event);
}

void NeoSlider::resizeEvent(QResizeEvent *event)
{
    NEO_PUBLIC_POINT(NeoSlider)
    d->updateWidgetGeometry(event->size());
    return QSlider::resizeEvent(event);
}

void NeoSlider::paintEvent(QPaintEvent *event)
{
    NEO_PUBLIC_POINT(NeoSlider)
    NEO_PAINTER_RESET
    d->drawBackground(&painter);
    d->drawTrack(&painter);
    d->drawIndicator(&painter);
}